package view;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;

import model.Day;
import model.TimeSlot;

/**
 * This class will take care of the specific work associated
 * with allowing the user to enter a users availability. 
 * Each of these cells will be laid out in the availability 
 * grid and observes itself and will respond when clicked.
 * 
 * Essentially just setting up a small tri-state state machine.
 * 
 * @author Tristan D. Boucher
 * @version Commit #22 9/14/2013
 */
@SuppressWarnings("serial")
public class AvailabilityCell extends AbstractCell 
{
  /**
   * Flag that determines whether this panel can change or not.
   */
  private final boolean my_changeable;
  
  /**
   * The width of this panel.
   */
  private final int my_width;
  
  /**
   * The height of this panel.
   */
  private final int my_height;
  
  /**
   * Constructor seeds the state machine by setting its state
   * to not selected and empty.
   * 
   * @param the_slot The time slot object associated with this cell.
   * @param the_day The day associated with this cell.
   * @param the_width The width of this components containing component.
   * @param the_height The height of this components containing component.
   */
  public AvailabilityCell(final TimeSlot the_slot,
                          final Day the_day,
                          final int the_width, 
                          final int the_height)
  {
    super(NOT_SELECTED, the_slot);
    this.addMouseListener(new MyMouseAdapter());
    
    my_changeable = true;
    
    my_width = the_width;
    my_height = the_height;
    my_day = the_day;
  }
  
  /**
   * Constructor seeds the state machine by setting its state
   * to not selected and empty as well as eliminates the ability
   * of this cell to change because it is being filled with text.
   * 
   * @param the_text The text to fill the cell with.
   * @param the_width The width of this panel.
   * @param the_height The height of this panel.
   */
  public AvailabilityCell(final String the_text, 
                          final int the_width, 
                          final int the_height)
  {
    super(NOT_SELECTED, TimeSlot.EMPTY);
    this.add(new JLabel(the_text));
    
    my_changeable = false;
    
    my_width = the_width;
    my_height = the_height;

  }

  /**
   * Paints the general path, in two different colors (one for old
   * paths, one for the current one).
   * 
   * @param the_graphics The graphics context to use for painting.
   */
  public void paintComponent(final Graphics the_graphics)
  {
    super.paintComponent(the_graphics);
    final Graphics2D g2d = (Graphics2D) the_graphics;

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                         RenderingHints.VALUE_ANTIALIAS_ON);

    //g2d.setStroke(new BasicStroke());
    if (my_changeable)
    {
      g2d.setColor(my_color);
      //This works but I have no idea how. Layout Managers are complicated.
      g2d.fillRect(0, 0, my_width, my_height);
    }
  }
  
  /**
   * This is the Mouse listener. Used MouseAdapter so I didn't have 
   * to write in all the overridden methods. MouseAdapter does it for me.
   * 
   * @author Tristan D. Boucher
   * @version Commit #12 9/7/2013
   */
  private class MyMouseAdapter extends MouseAdapter 
  {  
    /**
     * Overridden method from interface MouseAdapter.
     * @param the_event The event that was caused by the mouse.
     */
    public void mouseClicked(final MouseEvent the_event)   
    {    
      if (my_color == NOT_SELECTED)
      {
        my_color = SELECTED;
        getTime().setPreferred(false);
      }
      else
      {
        if (my_color == PREFERRED)
        {
          my_color = NOT_SELECTED;
          my_slot = TimeSlot.EMPTY;
          getTime().setPreferred(false);
        }
        else
        {
          my_color = PREFERRED;
          getTime().setPreferred(true);
        }
      }
      repaint();
    } 
  }
}
